<template>
    <div class="p-2">
        <!-- <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="data.queryParams" :inline="true">
                        <el-form-item label="资源类型名称" prop="resourceTypeName" label-width="96px">
                            <el-input v-model="data.queryParams.resourceTypeName" placeholder="请输入模板名称" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition> -->

        <el-card shadow="hover">
            <template #header>
                <el-row :gutter="10">
                    <el-col :span="1.5">
                        <el-button type="primary" :icon="Plus" plain @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" :icon="Delete" plain @click="handleDelete"
                            :disabled="selectionList.length === 0">删除</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table ref="roleTableRef" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="资源类型名称" prop="resourceTypeName" :show-overflow-tooltip="true" />
                <el-table-column label="操作" align="center" width="150">
                    <template #default="scope">
                        <el-button type="primary" :icon="Edit" size="small" @click="editRow(scope.row)" />
                        <el-button type="danger" :icon="Delete" size="small" @click="delRow(scope.row)" />
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="data.queryParams.total > 0" v-model:page="data.queryParams.pageNum"
                v-model:limit="data.queryParams.pageSize" :total="data.queryParams.total" @pagination="getList" />

        </el-card>
        <el-dialog v-model="dialogVisible" :title="actionType === 'add' ? '新增' : '修改'" width="500"
            :before-close="handleClose">
            <el-form :model="addForm" ref="ruleFormRef" label-width="106px" :rules="rules">
                <el-form-item label="资源类型名称" prop="resourceTypeName">
                    <el-input v-model="addForm.resourceTypeName" placeholder="请输入资源类型名称" />
                </el-form-item>
                <el-form-item>

                    <el-button @click="handleClose">取消</el-button>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        确定
                    </el-button>
                </el-form-item>
            </el-form>

        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { addResourceType, getResourceTypeList, deleteResourceType, updateResourceType } from "@/api/resources/calendarManagementApi"
import { Delete, Plus, Edit } from '@element-plus/icons-vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
const { proxy } = getCurrentInstance() as ComponentInternalInstance

const loading = ref(true)
const showSearch = ref(true)
const dataList = ref([])
const selectionList = ref([])
const dialogVisible = ref(false)
const actionType = ref('add')
const ruleFormRef = ref<FormInstance>()
const addForm = reactive({
    resourceTypeName: '',
})
const rules = reactive<FormRules>({
    resourceTypeName: [
        { required: true, message: '请输入资源类型名称', trigger: 'blur' },
    ],

})

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        resourceTypeName: '',
    },
})



/**
 * 查询模板列表
 */
const getList = async () => {
    loading.value = true
    const res: any = await getResourceTypeList({
        pageNum: data.queryParams.pageNum,
        pageSize: data.queryParams.pageSize,
        resourceTypeName: data.queryParams.resourceTypeName
    })
    if (res.code == 200) {
        loading.value = false
        dataList.value = res.data
        // data.queryParams.total = res.data.total
    }
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
    data.queryParams.pageNum = 1
    getList()
}

/** 重置 */
const resetQuery = () => {
    data.queryParams.resourceTypeName = ''
    handleQuery()
}
const handleSelectionChange = (val) => {
    selectionList.value = val
}
const handleAdd = () => {
    dialogVisible.value = true
    actionType.value = 'add'
}
const editRow = (row) => {
    dialogVisible.value = true
    actionType.value = 'edit'
    Object.assign(addForm, row)
}
const delRow = async (row) => {
    ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await deleteResourceType([row.resourceId])
        if (res.code == 200) {
            ElMessage.success('删除成功')
            getList()
        }
    })
}
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            if (actionType.value == 'add') {
                const res = await addResourceType({ ...addForm })
                if (res.code == 200) {
                    ElMessage.success('新增成功')
                    getList()
                    handleClose()
                }
            } else {
                const res = await updateResourceType({ ...addForm })
                if (res.code == 200) {
                    ElMessage.success('修改成功')
                    getList()
                    handleClose()
                }
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleClose = () => {
    dialogVisible.value = false
    ruleFormRef.value?.resetFields()
}
/**删除按钮操作 */
const handleDelete = async () => {
    ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const ids = selectionList.value.map((item) => item.resourceId)
        const res = await deleteResourceType(ids)
        if (res.code == 200) {
            ElMessage.success('删除成功')
            getList()
        }
    }).catch(() => {
        ElMessage.info('已取消删除')
    })
}


onMounted(() => {
    getList()
})
</script>